package LuoGu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class P1902 {
    static int[][] a;
    static int n;
    static int m;

    public static boolean ifOK(int y, int x, int max, int tot) {
        if (tot > max) {
            return false;
        }
        if (y == n)
            return true;
        if (x == 1)
            return ifOK(x + 1, y, max, Math.max(tot, a[x + 1][y])) && ifOK(x, y + 1, max, Math.max(tot, a[x][y + 1]));
        else if (x == m)
            return ifOK(x, y + 1, max, Math.max(tot, a[x][y + 1]));
        else {
            return ifOK(x + 1, y, max, Math.max(tot, a[x + 1][y])) && ifOK(x, y + 1, max, Math.max(tot, a[x][y + 1]))
                    && ifOK(x - 1, y, max, Math.max(tot, a[x - 1][y]));
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String tmp = br.readLine();
        n = Integer.parseInt(tmp.split(" ")[0]);
        m = Integer.parseInt(tmp.split(" ")[1]);
        a = new int[n + 1][m + 1];
        int max = Integer.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            tmp = br.readLine();
            String[] strs = tmp.split(" ");
            for (int j = 1; j <= m; j++) {
                a[i][j] = Integer.parseInt(strs[j - 1]);
                max = Math.max(max, a[i][j]);
            }
        }
        int left = 0;
        int right = max;
        int output = 0;
        while (left <= max) {
            int mid = (left + right) >>> 1;
            if (ifOK(1, 1, mid, 0)) {
                output = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        System.out.println(output);

    }
}
